package com.link.base.base.appinfo.service;

import javax.annotation.Resource;

import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;
import com.link.base.base.appinfo.dao.mybatis.mapper.AppPushConfigMapper;
import com.link.base.base.appinfo.model.AppPushConfig;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.List;

/**
 * 移动推送参数-维护移动消息推送相关的参数 
 *
 * @author ouyangzhenqiang
 * @date 2019/07/17 14:25
 */
@Service
public class AppPushConfigServiceImpl extends BasicServiceImpl<AppPushConfig> implements AppPushConfigService {

    @Resource
    private AppPushConfigMapper appPushConfigMapper;
    
    @Override
    public BasicMapper<AppPushConfig> getBasicMapper() {
        return appPushConfigMapper;
    }

    @Override
    public void insert(AppPushConfig entity) throws Exception {
        int num = appPushConfigMapper.checkConfigKey(entity);
        if (num > 0) {
            throw new BasicServiceException("参数键重复，请确认！");
        }
        super.insert(entity);
    }

    @Override
    public void setAppPushConfig() throws Exception {
        LinkRedisClient j = null;
        try {
            j = RedisUtil.getJedis();
            String redisKey = "APP_PUSH_CONFIG";
            AppPushConfig appPushConfig = new AppPushConfig();
            List<AppPushConfig> list = appPushConfigMapper.queryAll(appPushConfig);
            for (AppPushConfig item: list) {
                if (StringUtils.isNotBlank(item.getConfigKey()) && StringUtils.isNotBlank((item.getConfigVal()))) {
                    j.hset(redisKey, item.getConfigKey(), item.getConfigVal());
                }
            }
        } finally {
            RedisUtil.returnResource(j);
        }
    }
}

